#ifndef DEMOPROTOCOL_H
#define DEMOPROTOCOL_H

#include <qqtprotocol.h>
#include <qqtserialport.h>

/**
 * @brief The DEMOMessage class
 * DEMO报文
 */
class DEMOMessage : public QQtMessage
{
    Q_OBJECT
public:
    explicit DEMOMessage ( QObject* parent = nullptr );

    quint8& productBytes();
    quint8& commandBytes();
    quint16& paramBytes();
    const quint8& productBytes() const { return mProductBytes;}
    const quint8& commandBytes() const { return mCommandBytes; }
    const quint16& paramBytes() const {return mParamBytes;}

private:
    quint16 startBytes;
    quint32 addressBytes;
    quint8 mProductBytes;
    quint8 mCommandBytes;
    quint16 mParamBytes;
    quint16 stopBytes;

    // QQtMessage interface
public:
    virtual void parser ( const QByteArray& pkg ) override;

    virtual void packer ( QByteArray& l ) const override;
};

QDebug& operator<< ( QDebug& dbg, const DEMOMessage& msg );


/**
 * @brief The DEMOProtocol class
 * DEMO协议
 */
class DEMOProtocol : public QQtProtocol
{
    Q_OBJECT
public:
    explicit DEMOProtocol ( QObject* parent = nullptr );
    //用户调用形式2

    //用户调用形式1
    quint8& productCode() { return mMsg.productBytes(); }
    quint8& commandCode() { return mMsg.commandBytes(); }
    quint16& paramCode() { return mMsg.paramBytes(); }
    void sendLuwMessage() {
        QByteArray l;
        mMsg.packer ( l );
        write ( l );
    }
signals:

public slots:

protected:
    void recvBackMessage ( const DEMOMessage& msg ) {
        //不做处理
    }

private:
    DEMOMessage mMsg;

    // QQtProtocol interface
protected:
    virtual quint16 minlength() override {
        return 0x0A;
    }
    virtual quint16 maxlength() override {
        return 0x0C;
    }
    virtual quint16 splitter ( const QByteArray& stream ) override {
        return 0x0C;
    }
    virtual bool dispatcher ( const QByteArray& pkg ) override {
        DEMOMessage msg;
        msg.parser ( pkg );
        pline() << msg;
        recvBackMessage ( msg );
    }
};

/**
 * DEMO串口通讯句柄
 * @brief DEMOSerialPort
 * @param parent
 * @return
 */
DEMOProtocol* DEMOSerialPort ( QObject* parent = 0 );

#endif // DEMOPROTOCOL_H
